package com.yx.cloud.activity;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.iot.base.service.req.RenameDeviceParam;
import com.iot.common.utils.Event;
import com.iot.common.utils.LogUtils;
import com.iot.product.sweeper.bean.Robot;
import com.iot.sweeper.AppContext;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.LoadProgressDialog;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.ToastUtil;
import com.yx.cloud.view.NoSlidingViewPager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 配网连接成功
 */
public class ConnectSuccessActivity extends BaseActivity {
    private final static String TAG = "ConnectSuccessActivity";
    private NoSlidingViewPager mViewPager;
    //容器
    private List<View> mList = new ArrayList<>();
    private View view1, view2, view3;
    //小圆点
    private ImageView point1, point2, point3, mIvNewNameIcon;
    //跳过
    private Button mBtnConnectSuccessNextStep;
    private EditText mEtRobotName;
    private Robot robot;
    private LoadProgressDialog loadProgressDialog;
    private JSONObject productConfig;

    @Override
    public void onCreate() {
        setContentView(R.layout.connectsuccess_activity);
        EventBus.getDefault().register(this);
        productConfig = AppContext.getConfig();
        initView();
        initData();
    }

    //初始化View
    private void initView() {
//        robot = AppContext.get().getCurrentDevice();
//        String deviceId = robot.getDeviceId();
//        Log.d(TAG, "--新设备-------------- " + deviceId);

        mBtnConnectSuccessNextStep = findViewById(R.id.btn_connectSuccessNextStep);

        point1 = findViewById(R.id.iv_point1);
        point2 = findViewById(R.id.iv_point2);
        point3 = findViewById(R.id.iv_point3);

        //设置默认图片
        setPointImg(true, false, false, false);
        mViewPager = (NoSlidingViewPager) findViewById(R.id.mViewPager);

        view1 = View.inflate(this, R.layout.connectsuccess_item_one, null);

        view2 = View.inflate(this, R.layout.connectsuccess_item_two, null);
        ImageView home_env_pic = view2.findViewById(R.id.connectSuccess_home_icon);
        home_env_pic.setImageResource(productConfig.optInt(ProductConfigConstants.robot_home_env_2)); //第二张图  X1和其他机器不同

        view3 = View.inflate(this, R.layout.connectsuccess_item_three, null);
        mEtRobotName = view1.findViewById(R.id.et_setInitialRobotName);
        mIvNewNameIcon = view1.findViewById(R.id.iv_newNameIcon);
        // 根据pid判断机器型号，进行分类显示
        mIvNewNameIcon.setImageResource(productConfig.optInt(ProductConfigConstants.robot_Virtual));

        mList.add(view3);
        mList.add(view2);
        mList.add(view1);
        //设置适配器
        mViewPager.setAdapter(new GuideAdapter());
    }

    private void initData() {
        if (loadProgressDialog == null) {
            loadProgressDialog = new LoadProgressDialog(this);
        }
//        String deviceName = getIntent().getStringExtra("deviceName");
//        if (TextUtils.isEmpty(deviceName)) {
//            mEtRobotName.setText(productConfig.optString(ProductConfigConstants.default_robotName));
//        } else {
        LogUtils.i(" ---------------- 默认 名称 " + getString(Integer.parseInt(productConfig.optString(ProductConfigConstants.default_robotName))));
        mEtRobotName.setText(getString(Integer.parseInt(productConfig.optString(ProductConfigConstants.default_robotName))));
//        }

        mBtnConnectSuccessNextStep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RomUtils.isFastClick()) {
                    return;
                }
                String string = mBtnConnectSuccessNextStep.getText().toString();

                if (getString(R.string.N_Connect_Next).equals(string)) {
                    mViewPager.setCurrentItem(1);
                    setPointImg(false, true, false, false);
                    mBtnConnectSuccessNextStep.setText(getString(R.string.U_IKnow_Btn));

                } else if (getString(R.string.U_IKnow_Btn).equals(string)) {
                    mViewPager.setCurrentItem(2);
                    setPointImg(false, false, true, false);
                    mBtnConnectSuccessNextStep.setText(getString(R.string.N_Connect_Ok_Step2_Btn));
                } else if (getString(R.string.N_Connect_Ok_Step2_Btn).equals(string)) {
                    //配网成功后设置机器人名称
                    AppContext.get().setRecreate(2);
                    loadProgressDialog.show();
                    String mDeviceName = mEtRobotName.getText().toString().trim();
                    if (mDeviceName.equals("")) {
                        mDeviceName = "花花";
                    }
                    RenameDeviceParam param = new RenameDeviceParam();
                    param.setName(mDeviceName);
                    AppContext.getProductService().renameDevice(param);
                }
            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d(TAG, "---------------- " + position);
                if (position == 0) {
                    mBtnConnectSuccessNextStep.setText(getString(R.string.N_Connect_Next));
                    setPointImg(true, false, false, false);
                } else if (position == 1) {
                    mBtnConnectSuccessNextStep.setText(getString(R.string.U_IKnow_Btn));
                    setPointImg(false, true, false, false);
                } else if (position == 2) {
                    mBtnConnectSuccessNextStep.setText(getString(R.string.N_Connect_Ok_Step2_Btn));
                    setPointImg(false, false, true, false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_RENAME_DEVICE:
                boolean result = event.isResult();
                LogUtils.i("初始机器名称 结果 ： " + result);
                if (loadProgressDialog != null) {
                    loadProgressDialog.dismiss();
                }
                if (!result) {
                    ToastUtil.show(this, getString(R.string.N_Naming_Failure), Toast.LENGTH_SHORT);
                }
                Intent_utils.backIntent(ConnectSuccessActivity.this, MainBottomTabActivity.class);
                break;
        }
    }

    class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager) container).addView(mList.get(position));
            return mList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView(mList.get(position));
        }
    }

    // 设置小圆点的选中效果
    private void setPointImg(boolean isCheck1, boolean isCheck2, boolean isCheck3, boolean isCheck4) {
        if (isCheck1) {
            point1.setBackgroundResource(R.drawable.switch_track_on);
        } else {
            point1.setBackgroundResource(R.drawable.switch_track_off);
        }

        if (isCheck2) {
            point2.setBackgroundResource(R.drawable.switch_track_on);
        } else {
            point2.setBackgroundResource(R.drawable.switch_track_off);
        }

        if (isCheck3) {
            point3.setBackgroundResource(R.drawable.switch_track_on);
        } else {
            point3.setBackgroundResource(R.drawable.switch_track_off);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

